<?php

class FeedsLikesTb extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $feed_id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     * Independent Column Mapping.
     */
    public function getLikersFullname($feed_id)
    {
        $phql = "SELECT uat.fname,uat.lname FROM UserAccountTb uat
                 LEFT JOIN FeedsLikesTb flt on uat.id = flt.user_id
                 WHERE flt.feed_id = ?0";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$feed_id));
        $namestring = "";
        foreach ($data as  $value) {
            $namestring .= $value->fname.' '.$value->lname.'<br>';
        }
        return $namestring;
    }
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'feed_id' => 'feed_id', 
            'user_id' => 'user_id', 
            'date_created' => 'date_created'
        );
    }

    public function beforeValidationOnCreate()
    {
        $this->date_created = CURR_DATE;
    }

    public function countLikes($feed_id)
    {
        $phql = "SELECT COUNT(id) AS TOTAL FROM FeedsLikesTb WHERE feed_id = ?0";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$feed_id));
        return $data[0]->TOTAL;
    }
    public function checkIfLiked($feed_id,$user_id)
    {
        $feed = FeedsLikesTb::findFirst(array(
                "conditions"=>"feed_id = ?0 and user_id = ?1",
                "bind" => array(0=>$feed_id,1=>$user_id)
            )); 
        return $feed;
    }
}
